package com.ld.hpfs.service.impl;

import java.util.ArrayList;
import java.util.HashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ld.hpfs.dao.RuleMapper;
import com.ld.hpfs.service.RuleService;

@Service
public class RuleServiceImpl implements RuleService {
	private static final Logger logger = LoggerFactory
			.getLogger(DealServiceImpl.class);

	@Autowired
	private RuleMapper rulemapper;

	/*
	 * 增加规则
	 */
	@Override
	public int RuleSet(HashMap<String, Object> param) {
		return rulemapper.RuleSet(param);
	}

	/*
	 * 规则一览的查询
	 */
	@Override
	public ArrayList<HashMap<String, Object>> getRuleList(
			HashMap<String, Object> param) {
		int count = rulemapper.getRuleListCount(param);
		if (count == 0) {
			return null;
		} else {
			param.put("pageCount", count);
			return rulemapper.getRuleList(param);
		}
	}

	/*
	 * 规则一览的删除
	 */
	@Override
	public String deleteRuleList(HashMap<String, Object> param) {
		rulemapper.deleteRuleList(param);
		return "0";
	}

	/*
	 * 查询已选择规则
	 */
	@Override
	public ArrayList<HashMap<String, Object>> getCheckedRule(
			HashMap<String, Object> param) {
		try {
			return rulemapper.getCheckedRule(param);
		} catch (Exception ex) {
			logger.error(ex.getMessage());
			return null;
		}
	}

	/*
	 * 获取客户信息
	 */
	@Override
	public ArrayList<HashMap<String, Object>> getCustInfo(
			HashMap<String, Object> param) {
		try {
			return rulemapper.getCustInfo(param);
		} catch (Exception ex) {
			logger.error(ex.getMessage());
			return null;
		}
	}
	/*
	 * 获取创建者信息
	 */
	@Override
	public ArrayList<HashMap<String, Object>> getCreator(
			HashMap<String, Object> param) {
		try {
			return rulemapper.getCreator(param);
		} catch (Exception ex) {
			logger.error(ex.getMessage());
			return null;
		}
	}

	/*
	 * 获取共通条件
	 */
	@Override
	public ArrayList<HashMap<String, Object>> getCommonConditions(
			HashMap<String, Object> param) {
		try {
			return rulemapper.getCommonConditions(param);
		} catch (Exception ex) {
			logger.error(ex.getMessage());
			return null;
		}
	}
	
	/*
	 * 获取规则条件信息
	 */
	@Override
	public ArrayList<HashMap<String, Object>> getRuleConditions(
			HashMap<String, Object> param) {
		return rulemapper.getRuleConditions(param);
	}

	/*
	 * 获取自定义摘要信息
	 */
	@Override
	public ArrayList<HashMap<String, Object>> getAbstractInfo(
			HashMap<String, Object> param) {
		return rulemapper.getAbstractInfo(param);
	}

	/*
	 * 判断规则是否重复
	 */
	@Override
	public ArrayList<HashMap<String, Object>> checkRuleIsRepeat1(
			HashMap<String, Object> param) {
		return rulemapper.checkRuleIsRepeat1(param);
	}

	/*
	 * 判断规则是否重复
	 */
	@Override
	public ArrayList<HashMap<String, Object>> checkRuleIsRepeat2(
			HashMap<String, Object> param2) {
		return rulemapper.checkRuleIsRepeat2(param2);
	}

	/*
	 * 接收第三个框的值
	 */
	@Override
	public ArrayList<HashMap<String, Object>> getAdjustAfterRule(
			HashMap<String, Object> param) {
		return rulemapper.getAdjustAfterRule(param);
	}

	/*
	 * 清空原有sort
	 */
	@Override
	public void clearSort(HashMap<String, Object> param) {
		rulemapper.clearSort(param);
	}

	/*
	 * 更改SORT
	 */
	@Override
	public int updateSort(HashMap<String, Object> param) {
		return rulemapper.updateSort(param);
	}
 
	/*
	 * 获取替换规则
	 */
	@Override
	public ArrayList<HashMap<String, Object>> getReplaceRule(
			HashMap<String, Object> param) {
		return rulemapper.getReplaceRule(param);
	}

	@Override
	public ArrayList<HashMap<String, Object>> getReplaceUnit(
			HashMap<String, Object> param) {
		return rulemapper.getReplaceUnit(param);
	}

	/*
	 * 返利查询
	 */
	@Override
	public HashMap<String, Object> getRebate(HashMap<String, Object> param) {
		return rulemapper.getRebate(param);
	}

	/*
	 * 获取拆分规则后调整的值
	 */
	@Override
	public HashMap<String, Object> getSplitWay(HashMap<String, Object> param) {
		return rulemapper.getSplitWay(param);
	}

	/*
	 * 获得折扣信息
	 */
	@Override
	public HashMap<String, Object> getDiscoun(HashMap<String, Object> param) {
		return rulemapper.getDiscoun(param);
	}
 
	/*
	 * 规则履历添加的处理判断
	 */
	@Override
	public boolean addRuleHistory(HashMap<String, Object> history_param) {
		try {
			// 自定义摘要和备注获取履历
			if (history_param.get("ruleType").equals("31")
					|| history_param.get("ruleType").equals("32")) {
				this.AddRuleCommonHistory(history_param);
				ArrayList<HashMap<String, Object>> histories2 = rulemapper
						.getRuleAbstractRemark((Integer) history_param
								.get("ruleId"));
				for (HashMap<String, Object> history2 : histories2) {
					history2.put("action", history_param.get("action"));
					history2.put("user_id", history_param.get("user_id"));
					history2.put("user_name", history_param.get("user_name"));
					rulemapper.addRuleAbsRemHistory(history2);
				}
			} 
			// 单据状态获取履历
			else if (history_param.get("ruleType").equals("33")) {
				this.AddRuleCommonHistory(history_param);
				ArrayList<HashMap<String, Object>> histories10 = rulemapper
						.getRuleDocKeep((Integer) history_param.get("ruleId"));
				for (HashMap<String, Object> history10 : histories10) {
					history10.put("action", history_param.get("action"));
					history10.put("user_id", history_param.get("user_id"));
					history10.put("user_name", history_param.get("user_name"));
					rulemapper.addRuleDocKeepHistory(history10);
				}
			} 
			// 替换商品、客户、规格获取履历
			else if (history_param.get("ruleType").equals("21")
					|| history_param.get("ruleType").equals("22")
					|| history_param.get("ruleType").equals("24")) {
				this.AddRuleCommonHistory(history_param);
				ArrayList<HashMap<String, Object>> histories3 = rulemapper
						.getRuleReplaceOthers((Integer) history_param
								.get("ruleId"));
				for (HashMap<String, Object> history3 : histories3) {
					history3.put("action", history_param.get("action"));
					history3.put("user_id", history_param.get("user_id"));
					history3.put("user_name", history_param.get("user_name"));
					rulemapper.addRuleRplOthHistory(history3);
				}
			} 
			// 替换单位获取履历
			else if (history_param.get("ruleType").equals("23")) {
				this.AddRuleCommonHistory(history_param);
				ArrayList<HashMap<String, Object>> histories4 = rulemapper
						.getRuleReplaceUnit((Integer) history_param
								.get("ruleId"));
				for (HashMap<String, Object> history4 : histories4) {
					history4.put("action", history_param.get("action"));
					history4.put("user_id", history_param.get("user_id"));
					history4.put("user_name", history_param.get("user_name"));
					rulemapper.addRuleRplUnitHistory(history4);
				}
			} 
			// 特殊规则退货、发票分组、合并明细获取履历
			else if (history_param.get("ruleType").equals("11")
					|| history_param.get("ruleType").equals("15")
					|| history_param.get("ruleType").equals("16")) {
				this.AddRuleCommonHistory(history_param);
				this.AddRuleCommonAfHistory(history_param);
			} 
			// 特殊规则折扣获取履历
			else if (history_param.get("ruleType").equals("12")) {
				this.AddRuleCommonHistory(history_param);
				this.AddRuleCommonAfHistory(history_param);
				ArrayList<HashMap<String, Object>> histories6 = rulemapper.getRuleDiscount((Integer) history_param.get("ruleId"));
				for (HashMap<String, Object> history6 : histories6) {
					history6.put("action", history_param.get("action"));
					history6.put("user_id", history_param.get("user_id"));
					history6.put("user_name", history_param.get("user_name"));
					rulemapper.addRuleDiscountHistory(history6);
				}
			} 
			// 特殊规则返利获取履历
			else if (history_param.get("ruleType").equals("13")) {
				this.AddRuleCommonHistory(history_param);
				this.AddRuleCommonAfHistory(history_param);
				ArrayList<HashMap<String, Object>> histories7 = rulemapper
						.getRuleRebate((Integer) history_param.get("ruleId"));
				for (HashMap<String, Object> history7 : histories7) {
					history7.put("action", history_param.get("action"));
					history7.put("user_id", history_param.get("user_id"));
					history7.put("user_name", history_param.get("user_name"));
					rulemapper.addRuleRebateHistory(history7);
				}
			} 
			// 特殊规则拆分获取履历
			else if (history_param.get("ruleType").equals("14")) {
				this.AddRuleCommonHistory(history_param);
				this.AddRuleCommonAfHistory(history_param);
				ArrayList<HashMap<String, Object>> histories8 = rulemapper
						.getRuleSplit((Integer) history_param.get("ruleId"));
				for (HashMap<String, Object> history8 : histories8) {
					history8.put("action", history_param.get("action"));
					history8.put("user_id", history_param.get("user_id"));
					history8.put("user_name", history_param.get("user_name"));
					rulemapper.addRuleSplitHistory(history8);
				}
			} else {
				this.AddRuleCompanyHistory(history_param);
			}
			return true;
		} catch (Exception ex) {
			logger.error(ex.getMessage());
			return false;
		}
	}

	/*
	 * 对规则主表和规则条件表做插入操作
	 */
	public void AddRuleCommonHistory(HashMap<String, Object> history_param) {
		ArrayList<HashMap<String, Object>> histories = rulemapper
				.getRuleMain((Integer) history_param.get("ruleId"));
		for (HashMap<String, Object> history : histories) {// 得到规则主表中ruleId为获取Id的那一行
			history.put("action", history_param.get("action"));
			history.put("user_id", history_param.get("user_id"));
			history.put("user_name", history_param.get("user_name"));
			rulemapper.addRuleMainHistory(history);
		}

		ArrayList<HashMap<String, Object>> histories1 = rulemapper
				.getRuleCondition((Integer) history_param.get("ruleId"));
		for (HashMap<String, Object> history1 : histories1) {// 得到规则选择表中ruleId为获取Id的那一行
			history1.put("action", history_param.get("action"));
			history1.put("user_id", history_param.get("user_id"));
			history1.put("user_name", history_param.get("user_name"));
			rulemapper.addRuleConditionHistory(history1);
		}
	}

	/*
	 * 对规则处理后调整表做插入操作
	 */
	public void AddRuleCommonAfHistory(HashMap<String, Object> history_param) {
		ArrayList<HashMap<String, Object>> histories5 = rulemapper
				.getRuleAdjustAfter((Integer) history_param.get("ruleId"));
		for (HashMap<String, Object> history5 : histories5) {// 得到规则主表中ruleId为获取Id的那一行
			history5.put("action", history_param.get("action"));
			history5.put("user_id", history_param.get("user_id"));
			history5.put("user_name", history_param.get("user_name"));
			rulemapper.addRuleAfterHistory(history5);
		}
	}

	/*
	 * 对规则选择表做插入操作
	 */
	public void AddRuleCompanyHistory(HashMap<String, Object> history_param) {
		ArrayList<HashMap<String, Object>> histories9 = rulemapper
				.getRuleCompany();
		for (HashMap<String, Object> history9 : histories9) {// 得到规则主表中ruleId为获取Id的那一行
			history9.put("action", history_param.get("action"));
			history9.put("user_id", history_param.get("user_id"));
			rulemapper.addRuleCompanyHistory(history9);
		}
	}

	/*
	 * 规则主表导入
	 */
	@Override
	public HashMap<String, Object> importRuleMain(HashMap<String, Object> param) {
		return rulemapper.importRuleMain(param);
	}

	/*
	 * 规则条件表导入
	 */
	public int importRuleCondition(HashMap<String, Object> param2) {
		return rulemapper.importRuleCondition(param2);
	}

	/*
	 * 自定义摘要、备注信息导入
	 */
	public int importAbstractRemark(HashMap<String, Object> param3) {
		return rulemapper.importAbstractRemark(param3);
	}
	
	/*
	 * 单据状态信息导入
	 */
	public int importRuleDocStatus(HashMap<String, Object> param3) {
		return rulemapper.importRuleDocStatus(param3);
	}
	
	/*
	 * 替换商品名、替换客户名和替换规格信息导入
	 */
	public int importReplaceGoodsCustType(HashMap<String, Object> param3) {
		return rulemapper.importReplaceGoodsCustType(param3);
	}

	/*
	 * 替换单位信息导入
	 */
	public int importReplaceUnit(HashMap<String, Object> param3) {
		return rulemapper.importReplaceUnit(param3);
	}

	/*
	 * 特殊规则信息导入
	 */
	public int importSpecialRule(HashMap<String, Object> param3) {
		return rulemapper.importSpecialRule(param3);
	}

	/*
	 * 折扣信息导入
	 */
	public int importRuleDiscount(HashMap<String, Object> param3) {
		return rulemapper.importRuleDiscount(param3);
	}

	/*
	 * 返利信息导入
	 */
	public int importRuleRebate(HashMap<String, Object> param3) {
		return rulemapper.importRuleRebate(param3);
	}

	/*
	 * 拆分信息导入
	 */
	public int importRuleSplit(HashMap<String, Object> param3) {
		return rulemapper.importRuleSplit(param3);
	}

	/*
	 * Check客户是否存在
	 */
	public HashMap<String, Object> getCustomerName(
			HashMap<String, Object> checkParam) {
		return rulemapper.getCustomerName(checkParam);
	}

	/*
	 * check客户代码是否存在
	 */
	@Override
	public int checkCustCode(HashMap<String, Object> param) {
		return rulemapper.checkCustCode(param);
	}

	/*
	 * 导出ruleMain
	 */
	@Override
	public ArrayList<HashMap<String, Object>> getRuleMainData(
			HashMap<String, Object> param) {
		return rulemapper.getRuleMainData(param);
	}

	/*
	 * 导出ruleCondition
	 */
	@Override
	public ArrayList<HashMap<String, Object>> getRuleConditionData(
			HashMap<String, Object> param) {
		return rulemapper.getRuleConditionData(param);
	}

	/*
	 * 导出adjustAfterRule
	 */
	@Override
	public ArrayList<HashMap<String, Object>> getAdjustAfterRuleData(
			HashMap<String, Object> param) {
		return rulemapper.getAdjustAfterRuleData(param);
	}

	/*
	 * 导出discount规则
	 */
	@Override
	public ArrayList<HashMap<String, Object>> getRuleDiscountData(
			HashMap<String, Object> param) {
		return rulemapper.getRuleDiscountData(param);
	}

	/*
	 * 导出rebate规则
	 */
	@Override
	public ArrayList<HashMap<String, Object>> getRuleRebateData(
			HashMap<String, Object> param) {
		return rulemapper.getRuleRebateData(param);
	}

	/*
	 * 导出split规则
	 */
	@Override
	public ArrayList<HashMap<String, Object>> getRuleSplitData(
			HashMap<String, Object> param) {
		return rulemapper.getRuleSplitData(param);
	}

	/*
	 * 导出替换商品、客户、规格规则
	 */
	@Override
	public ArrayList<HashMap<String, Object>> getreplaceOthersData(
			HashMap<String, Object> param) {
		return rulemapper.getreplaceOthersData(param);
	}

	/*
	 * 导出替换单位规则
	 */
	@Override
	public ArrayList<HashMap<String, Object>> getreplaceUnitData(
			HashMap<String, Object> param) {
		return rulemapper.getreplaceUnitData(param);
	}

	/*
	 * 导出自定义摘要、备注规则
	 */
	@Override
	public ArrayList<HashMap<String, Object>> getCustomizeRemarkData(
			HashMap<String, Object> param) {
		return rulemapper.getCustomizeRemarkData(param);
	}
	
	/*
	 *  导出单据状态
	 */
	@Override
	public ArrayList<HashMap<String, Object>> getDocTypeData(
			HashMap<String, Object> param) {
		return rulemapper.getDocTypeData(param);
	}

	@Override
	public ArrayList<HashMap<String, Object>> getRuleSort(
			HashMap<String, Object> param) {
		return rulemapper.getRuleSort(param);
	}

	/*
	 * 获得规则导出的规则ID
	 */
	@Override
	public ArrayList<HashMap<String, Object>> getExportId(
			HashMap<String, Object> listParam) {
		return rulemapper.getExportId(listParam);
	}

	/*
	 * 修改页面---获取单据状态的值
	 */
	@Override
	public HashMap<String, Object> getDocType(HashMap<String, Object> param) {
		return rulemapper.getDocType(param);
	}
	
	/*
	 * 导入公司代码CHECK,当前登录公司编码的获取
	 */
	@Override
	public HashMap<String, Object> getCompanyCode(HashMap<String, Object> param) {
		return rulemapper.getCompanyCode(param);
	}

	@Override
	public int insertRules(HashMap<String, Object> param) {
		int detail_id = rulemapper.getMaxDetailId(param.get("ruleType").toString());
		
		param.put("detail_id", detail_id);
		int maxRuleId = rulemapper.getMaxRuleId(param);
		param.put("rule_id", maxRuleId);
		
		rulemapper.insertRules(param);
		return 0;
	}

	@Override
	public int updateRules(HashMap<String, Object> param) {
		rulemapper.updateRules(param);
		return 0;
	}

	@Override
	public int getLockStatus(HashMap<String, Object> param) {
		// TODO Auto-generated method stub
		return rulemapper.getLockStatus(param);
	}

	@Override
	public int getMaxDetailId(String fileName1) {
		// TODO Auto-generated method stub
		return rulemapper.getMaxDetailId(fileName1);
	}

}
